/*
 * Copyright (c) 2021 Zsj. All rights reserved.
 */

package com.jzz.crawlerweb.controller;

import com.jzz.crawlerweb.handler.util.CaptchaUtil;
import com.jzz.crawlerweb.service.TextCaptchaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/api/text")
public class TextCaptchaController {

    @Autowired
    private CaptchaUtil captchaUtil;

    @Autowired
    private TextCaptchaService textCaptchaService;

    @PostMapping("/login")
    public String login(Model model,
                        HttpServletRequest request,
                        @RequestParam String username,
                        @RequestParam String password) {
        model.addAttribute("un", username);
        model.addAttribute("pwd", password);
        model.addAttribute("secret", captchaUtil.getSecret(request));
        return "success";
    }

    @ResponseBody
    @GetMapping(value = "/code", produces = MediaType.IMAGE_JPEG_VALUE)
    public byte[] code(HttpServletRequest request) {
        return textCaptchaService.code(request);
    }

}
